<template>
  <div>
    <ul v-if="payResult">
      <li><img src="../../../assets/offersuccess.png" alt="" width="40"></li>
      <div>
        <li>支付成功</li>
      </div>
      <li>
        <wv-button type="default" :mini="true" @click="goTurnUrl()">返回商家</wv-button>
      </li>
    </ul>
    <ul v-else>
      <div>
        <li><img src="../../../assets/fail.jpg" alt="" width="181"></li>
        <li>支付失败</li>
      </div>
    </ul>
  </div>
</template>

<script type="text/ecmascript-6">
  import {orderPayResult, getAccountGoods} from '../../../../test/unit/http'

  export default {
    name: 'payresultgold',
    props: {},
    data() {
      return {
        payResult: false
      };
    },
    methods: {
      goTurnUrl() {
        /*https://testvip.dr009.com/#/payresultgold?payTime=2021-07-15 13:05:54&buyerCashPayAmt=1&connectSys=UNIONPAY&sign=97C4DA2B34CDE7419E6EE83B5F835A70EBCB8C12F345AB89C7C41F4BB53EAF1A&merName=北京德人企业管理有限公司&mid=89810007392013N&invoiceAmount=1&settleDate=2021-07-15&billFunds=现金:1&buyerId=otdJ_uFdAvg4yE0eQqAisv-bHkos&mchntUuid=2d9081bc7a511e03017a5245108622d2&tid=DM062671&instMid=YUEDANDEFAULT&receiptAmount=1&couponAmount=0&attachedData={}&targetOrderId=4200001184202107157983361526&OG=TZkO&signType=SHA256&billFundsDesc=现金支付0.01元。&orderDesc=北京德人企业管理有限公司&seqId=22503207678N&merOrderId=129H3DD3DA82471D8FF19E04E605051E&targetSys=WXPay&bankInfo=OTHERS&totalAmount=1&createTime=2021-07-15 13:05:49&buyerPayAmount=1&notifyId=557adb69-406f-4bc9-8f4e-6202844b4366&subInst=100000&status=TRADE_SUCCESS*/
        /*https://testvip.dr009.com/#/payresultgold?payTime=2021-07-19%2013%3A06%3A15&connectSys=UNIONPAY&sign=B417F6120B1F9030B6B9BCC98F110547E2971F1DD80D104EA9D2104771FF50A0&merName=%E5%8C%97%E4%BA%AC%E5%BE%B7%E4%BA%BA%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%86%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&mid=89810007392013N&settleDate=2021-07-19&mchntUuid=2d9081bc7a511e03017a5245108622d2&tid=DM062671&instMid=YUEDANDEFAULT&EU=jOqJ&totalAmount=1&couponAmount=0&attachedData=%7B%7D&createTime=2021-07-19%2013%3A06%3A15&signType=SHA256&notifyId=af4f4a61-8bab-46a1-80ce-fdd1770f6c2e&subInst=100000&orderDesc=%E5%8C%97%E4%BA%AC%E5%BE%B7%E4%BA%BA%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%86%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&seqId=22573237460N&merOrderId=129HF1B4490645E095A7D23393CE9F8B&status=WAIT_BUYER_PAY&targetSys=WXPay*/
        let payInfoId = this.$myUtils.GetUrlParam("merOrderId");
        let params = {payInfoId: payInfoId};
        this.getOrderPayResult(params);
      },
      turnToUrl(turnUrl) {
        let mchData = {action: 'jumpOut', jumpOutUrl: turnUrl}
        let postData = JSON.stringify(mchData)
        parent.postMessage(postData, 'https://payapp.weixin.qq.com')
      },
      getOrderPayResult(params) {
        let baseUrl = this.$myUtils.getBaseUrl();
        orderPayResult(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            let payResult = res.object.payResult;
            if (payResult) {
              let orderdetailList = res.object.orderdetailList;
              let length = orderdetailList.length;
              if (length == 1) {
                let orderdetail = orderdetailList[0];
                if (orderdetail.categoryCode == 'vipcard_2') {
                  if (orderdetail.buyNum == 1) {
                    this.getAccountGoodsData({orderdetailId: orderdetail.orderdetailId});
                  } else {
                    let turnUrl = baseUrl + '/#/myguibinka'
                    this.turnToUrl(turnUrl)
                  }
                }
                if (orderdetail.categoryCode == 'vipcard_1') {
                  let turnUrl = baseUrl + '/#/mylipinka'
                  this.turnToUrl(turnUrl)
                }
              }
            }
          } else {
            let turnUrl = baseUrl + '/#/index'
            this.turnToUrl(turnUrl)
          }
        })
      },
      getAccountGoodsData(params) {
        getAccountGoods(params).then(res => {
          let baseUrl = this.$myUtils.getBaseUrl();
          console.log(res);
          if (res.code == 100) {
            let accountGoodsId = res.object.accountGoodsId;
            let vipcardCategoryCode = res.object.vipcardCategoryCode;
            if (vipcardCategoryCode == 'VIP') {
              let turnUrl = baseUrl + '/#/wanshanziliao?accountGoodsId=' + accountGoodsId + '&type=1'
              this.turnToUrl(turnUrl)
            }
            if (vipcardCategoryCode == 'GUEST') {
              let turnUrl = baseUrl + '/#/yaokewanshan?accountGoodsId=' + accountGoodsId + '&type=1'
              this.turnToUrl(turnUrl)
            }
          }
        })
      },
      goBack() {
        this.$router.replace({path: '/'});
      }
    },
    mounted() {
      if (window.history && window.history.pushState) {
        history.pushState(null, null, document.URL);
        window.addEventListener('popstate', this.goBack, false);
      }
      let mchData = {action: 'onIframeReady', displayStyle: 'SHOW_CUSTOM_PAGE', height: 500}
      let postData = JSON.stringify(mchData)
      parent.postMessage(postData, 'https://payapp.weixin.qq.com')
      let status = this.$myUtils.GetUrlParam("status");
      if (status == 'TRADE_SUCCESS') {
        this.payResult = true
      } else {
        this.payResult = false
      }
    },
    created() {
    },
    destroyed() {
      window.removeEventListener('popstate', this.goBack, false);
    },
    components: {}
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  ul {
    padding-top: 50px;
    text-align: center;

    li:nth-child(2) {
      font-size: 18px;
      color: #111;
      margin: 10px 0;
    }

    li:last-child {
      display: flex;
      padding: 10px;
      margin-top: 20px;
      justify-content: space-around;

      .weui-btn + .weui-btn {
        margin-top: 0;
      }

      button {
        flex: 1;
        font-size: 15px;
        border-radius: 25px;
        height: 40px;
        line-height: 40px;
      }

      :first-child {
        color: #2e94f0;
        background-color: #fff;
        margin-right: 10px;
        border: 1px solid #2e94f0;
      }

      :last-child {
        background-color: #2e94f0;
        color: #fff;
      }
    }
  }
</style>
